package cn.wolfcode.luowowo.wensite.web.controller;

import cn.wolfcode.luowowo.article.domain.Strategy;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.domain.DestinationTemplate;
import cn.wolfcode.luowowo.search.domain.StrategyTemplate;
import cn.wolfcode.luowowo.search.domain.TravelTemplate;
import cn.wolfcode.luowowo.search.domain.UserInfoTemplate;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import cn.wolfcode.luowowo.wensite.web.annotation.RequireLogin;
import cn.wolfcode.luowowo.wensite.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.naming.directory.SearchResult;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
public class IndexController {
    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private ITravelCommendService travelCommendService;
    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private ITravelService travelService;

    @Reference
    private IDestinationTemplateSearchService destinationTemplateSearchService;

    @Reference
    private ITravelTemplateSearchService travelTemplateSearchService;

    @Reference
    private IUserInfoTemplateSearchService userInfoTemplateSearchService;

    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;

    @Reference
    private ISearchService searchService;

    @RequestMapping("")
    @RequireLogin  //拦截有问题
    public String getIdnex(Model model, HttpServletRequest request) {
        String cookie = CookieUtil.getCookie(request);
        if (cookie != null) {
            //然后从redis中的缓存中拿出用户信息
            UserInfo userInfo = userInfoRedisService.getToken(cookie);
            request.getSession().setAttribute("userInfo", userInfo);
        }
        //tcs
        model.addAttribute("tcs", travelCommendService.getTop5());
        //sc
        model.addAttribute("sc", strategyCommendService.getTop5().get(0));
        return "index/index";
    }

    @RequestMapping("/index/travelPage")
    public String travelPage(Model model, @ModelAttribute("qo") TravelQuery qo) {
        model.addAttribute("pageInfo", travelService.query(qo));
        return "index/travelPageTpl";
    }

    @RequestMapping("/q")
    public String search(Model model, @ModelAttribute("qo") SearchQueryObject qo) {
        switch (qo.getType()) {
            case SearchQueryObject.SEARCH_TYPE_DEST:
                return searchDest(model, qo);
            case SearchQueryObject.SEARCH_TYPE_STRATEGY:
                return searchStrategy(model, qo);
            case SearchQueryObject.SEARCH_TYPE_TRAVEL:
                return searchTravel(model, qo);
            case SearchQueryObject.SEARCH_TYPE_USER:
                return searchUser(model, qo);
            default:
                return searchAll(model, qo);
        }
    }

    //所有
    private String searchAll(Model model, SearchQueryObject qo) {
        Page<UserInfoTemplate> us = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo,
                "nickname", "destName");
        Page<TravelTemplate> ts = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo,
                "title", "summary");
        Page<StrategyTemplate> sts = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo,
                "title", "subTitle", "summary");
        Page<DestinationTemplate> ds = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME, DestinationTemplate.class, qo,
                "name", "info");
        SearchResultVO data = new SearchResultVO();
        data.setTotal(ds.getTotalElements() + sts.getTotalElements() + ts.getTotalElements() + us.getTotalElements());
        data.setTravels(ts.getContent());
        data.setStrategys(sts.getContent());
        data.setUsers(us.getContent());
        data.setDests(ds.getContent());

        model.addAttribute("data", data);
        return "index/searchAll";
    }

    //用户
    private String searchUser(Model model, SearchQueryObject qo) {
        Page<UserInfoTemplate> page = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo,
                "nickname", "destName");
        model.addAttribute("page", page);
        return "index/searchUser";
    }

    //游记
    private String searchTravel(Model model, SearchQueryObject qo) {
        Page<TravelTemplate> page = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo,
                "title", "summary");
        model.addAttribute("page", page);
        return "index/searchTravel";
    }

    //攻略
    private String searchStrategy(Model model, SearchQueryObject qo) {
        Page<StrategyTemplate> page = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo,
                "title", "subTitle", "summary");
        model.addAttribute("page", page);
        return "index/searchStrategy";
    }

    //目的地
    private String searchDest(Model model, SearchQueryObject qo) {

        //通过关键字查询目的地是否存在
        DestinationTemplate dest = destinationTemplateSearchService.getDestByName(qo.getKeyword());
        if (dest != null) {
            //如果目的地存在,那就查询指定目的地下的攻略,游记用户,只查前5篇
            List<StrategyTemplate> sts = strategyTemplateSearchService.getDestByName(qo.getKeyword());
            SearchResultVO data = new SearchResultVO();
            data.setTotal(sts.size() + data.getTotal());
            //约定了只取前5个
            if (sts.size() > 5) {
                sts = sts.subList(0, 5);
            }
            List<TravelTemplate> ts = travelTemplateSearchService.getDestByName(qo.getKeyword());
            data.setTotal(ts.size() + data.getTotal());
            if (ts.size() > 5) {
                ts = ts.subList(0, 5);
            }
            List<UserInfoTemplate> us = userInfoTemplateSearchService.findByDestName(qo.getKeyword());
            data.setTotal(us.size() + data.getTotal());
            if (us.size() > 5) {
                us = us.subList(0, 5);
            }
            model.addAttribute("data", data);
            model.addAttribute("dest", dest);
        }
        return "index/searchDest";
    }
}
